﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Web;

namespace OF.Security
{
    /// <summary>
    /// 用户上下文
    /// </summary>
    [Serializable]
    public class UserContext : Dictionary<string, object>
    {
        /// <summary>
        /// 上下文Key
        /// </summary>
        public const string ContextKey = "OF.AppContext";
        /// <summary>
        /// ContextHeaderLocalName
        /// </summary>
        public const string ContextHeaderLocalName = "UserContext";
        /// <summary>
        /// ContextHeaderNamespace
        /// </summary>
        public const string ContextHeaderNamespace = "http://OF.AppContext/";
        /// <summary>
        /// 当前用户上下文
        /// </summary>
        public static UserContext Current
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Items[ContextKey] == null)
                    {
                        HttpContext.Current.Items[ContextKey] = new UserContext();
                    }
                    return HttpContext.Current.Items[ContextKey] as UserContext;
                }
                else
                {
                    if (CallContext.GetData(ContextKey) == null)
                    {
                        CallContext.SetData(ContextKey, new UserContext());
                    }
                    return CallContext.GetData(ContextKey) as UserContext;
                }
            }
            set
            {
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Items[ContextKey] = value; ;
                }
                else
                {
                    CallContext.SetData(ContextKey, value);
                }
            }
        }

        /// <summary>
        /// 当前用户是否认证登录
        /// </summary>
        public static bool IsAuthenticated
        {
            get
            {
                return !Current.UserID.Equals(0);
            }
        }


     
        /// <summary>
        /// 系统认证标识
        /// </summary>       
        public string SigninID
        {
            get
            {
                if (!this.ContainsKey("__SigninID"))
                {
                    return string.Empty;
                }
                return (string)this["__SigninID"];
            }
            set
            {
                this["__SigninID"] = value;
            }
        }
    

       
        /// <summary>
        /// 用户ID
        /// </summary>       
        public int UserID
        {
            get
            {
                if (!this.ContainsKey("__UserID"))
                {
                    return 0;
                }
                return int.Parse(this["__UserID"].ToString());
            }
            set
            {
                this["__UserID"] = value;
            }
        }
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get
            {
                if (!this.ContainsKey("__UserName"))
                {
                    return string.Empty;
                }
                return (string)this["__UserName"];
            }
            set
            {
                this["__UserName"] = value;
            }
        }
        /// <summary>
        /// 用户姓名
        /// </summary>
        public string RealName
        {
            get
            {
                if (!this.ContainsKey("__RealName"))
                {
                    return string.Empty;
                }
                return (string)this["__RealName"];
            }
            set
            {
                this["__RealName"] = value;
            }
        }

        public string AppID
        {
            get
            {
                if (!this.ContainsKey("AppID"))
                {
                    return string.Empty;
                }
                return (string)this["AppID"];
            }
            set
            {
                this["AppID"] = value;
            }
        }

        /// <summary>
        /// 访问者IP
        /// </summary>       
        public string IP
        {
            get
            {
                if (!this.ContainsKey("__IP"))
                {
                    return string.Empty;
                }
                return (string)this["__IP"];
            }
            set
            {
                this["__IP"] = value;
            }
        }

        /// <summary>
        /// 请求URL
        /// </summary>       
        public string RequestUrl
        {
            get
            {
                if (!this.ContainsKey("__RequestUrl"))
                {
                    return string.Empty;
                }
                return (string)this["__RequestUrl"];
            }
            set
            {
                this["__RequestUrl"] = value;
            }
        }

      
    }
}
